package com.quanquan.management.controller;

import DTO.PageDTO;
import base.BaseConst;
import com.quanquan.management.dto.ActivityDTO;
import com.quanquan.management.dto.AdsenceDTO;
import com.quanquan.management.dto.BannerDTO;
import com.quanquan.management.request.BannerRequest;
import com.quanquan.management.service.ActivityService;
import com.quanquan.management.service.AdsenceService;
import com.quanquan.management.service.BannerService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import web.response.ResultBody;

import java.util.List;

/**
 * @author gongzhou
 * @title: BannerController
 * @projectName SpringCloud
 * @description: TODO
 * @date 2019/4/81:22
 */
@Controller
@RequestMapping("/banner")
public class BannerController extends ManagerBaseController {

    private final BannerService bannerService;
    private final ActivityService activityService;
    private final AdsenceService adsenceService;

    @Autowired
    public BannerController(BannerService bannerService, ActivityService activityService, AdsenceService adsenceService) {
        this.bannerService = bannerService;
        this.activityService = activityService;
        this.adsenceService = adsenceService;
    }

    /**
     * 多条件查询 优惠券列表
     *
     * @param pageSize
     * @param page
     * @param keyword
     * @param sort
     * @param fromDate
     * @param toDate
     * @param isAsc
     * @param sta
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate,
                       @RequestParam(required = false) boolean isAsc,
                       @RequestParam(required = false) Integer sta,
                       Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        List<AdsenceDTO> adsenceDTOS = adsenceService.list();
        model.addAttribute("adsenceDTOS", adsenceDTOS);
        BannerRequest requestBody = new BannerRequest(page, pageSize, keyword, sort, isAsc);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setStatus(sta);
        PageDTO<BannerDTO> pageDTO = bannerService.list(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "banner/list";
    }


    @RequestMapping(value = "/edit")
    public String edit(String id, BannerRequest requestBody, Model model) {
        List<ActivityDTO> activityDTOS = activityService.list();
        model.addAttribute("activityDTOS", activityDTOS);
        List<AdsenceDTO> adsenceDTOS = adsenceService.list();
        model.addAttribute("adsenceDTOS", adsenceDTOS);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            BannerDTO byId = bannerService.findById(id);
            model.addAttribute("editable", byId);
            return "banner/form";
        }
        boolean update = bannerService.update(requestBody);
        if (update) {
            return "redirect:/banner/list";
        }
        return null;
    }

    @RequestMapping(value = "/delete")
    public String delete(String id) {
        boolean delete = bannerService.delete(id);
        if (delete) {
            return "redirect:/banner/list";
        }
        return null;
    }

    /**
     * 新增
     * @param requestBody
     * @return
     */
    @RequestMapping(value = "/add")
    public String add(BannerRequest requestBody, Model model) {
        List<ActivityDTO> activityDTOS = activityService.list();
        model.addAttribute("activityDTOS", activityDTOS);
        List<AdsenceDTO> adsenceDTOS = adsenceService.list();
        model.addAttribute("adsenceDTOS", adsenceDTOS);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            return "banner/form";
        }
        boolean add = bannerService.add(requestBody);
        if (add) {
            model.addAttribute("error",false);
            return "redirect:/banner/list";
        }
        model.addAttribute("error",true);
        return "banner/form";
    }

    /**
     * 上下架
     *
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/updateStatus")
    @ResponseBody
    public ResultBody updateStatus(@RequestParam String id, @RequestParam Integer status) {
        BannerRequest requestBody = new BannerRequest();
        requestBody.setId(id);
        requestBody.setStatus(status);
        Integer changeStatus = null;
        if (status == 1) {
            changeStatus = 0;
        }
        if (status == 0) {
            changeStatus = 1;
        }
        boolean b = bannerService.updateStatus(requestBody);
        if (b) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),changeStatus);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
    }
}
